<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品展示列表</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>
</head>
<body>
<div class="bookContainer">
    <h2>商品展示列表</h2>
    <div class="navbar-justify-between">
        <div>
            <button class="btn btn-outline-info" type="button" onclick="location.href='add.html'">添加商品</button>
        </div>
    </div>
    <table>
        <thead>
        <tr>
            <td>商品ID</td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>商品库存</td>
            <td class="width200">操作</td>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>

    <div class="demo">
        <ul id="pageContainer" class="pagination justify-content-center"></ul>
    </div>
    <script>

        getGoodsList();


        function getGoodsList() {
            $.ajax({
                type: "get",
                url: "/good/getGoodsListByPage" + location.search,
                success: function (result) {
                    var data = result.records;
                    var finalHtml = "";
                    for (var good of data) {
                        //拼接html
                        finalHtml += '<tr>';
                        finalHtml += '<td>' + good.id + '</td>';
                        finalHtml += '<td>' + good.goodName + '</td>';
                        finalHtml += '<td>' + good.goodPrice + '</td>';
                        finalHtml += '<td>' + good.goodNum + '</td>';
                        finalHtml += '<td>';
                        finalHtml += '<div class="op">';
                        finalHtml += '<a href="javascript:void(0)" onclick="deleteGood(' + good.id + ')">删除</a>';
                        finalHtml += '</div>';
                        finalHtml += '</td>';
                        finalHtml += '</tr>';
                    }

                    $("tbody").html(finalHtml);

                    //处理翻页信息
                    //翻页信息
                    $("#pageContainer").jqPaginator({
                        totalCounts: result.count, //总记录数
                        pageSize: 4,    //每页的个数
                        visiblePages: 3, //可视页数
                        currentPage: result.pageRequest.currentPage,  //当前页码
                        first: '<li class="page-item"><a class="page-link">首页</a></li>',
                        prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                        next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                        last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                        page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                        onPageChange: function (page, type) {
                            console.log("第" + page + "页, 类型:" + type);
                            if (type == "change") {
                                location.href = "order_list.html?currentPage=" + page;
                            }

                        }
                    });
                }
            });
        }


        function deleteGood(id) {
            var isDelete = confirm("确认删除?");//弹出确认框
            if (isDelete) {
                console.log(id);
                //删除图书
                $.ajax({
                    type: "post",
                    url: "/good/deleteByGoodId",
                    data: {
                        "id": id,
                    },
                    success: function (result) {
                        if (result) {
                            alert("删除成功");
                            location.href = "order_list.html";
                        }else{
                            alert("删除失败")
                        }
                    }
                });
            }
        }
    </script>
</div>
</body>

</html>